import ScmsaAxios from 'scmsa-axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
import {
  getToken,
  getTokenType,
  getRefreshToken,
  setToken,
  setTokenType,
  setRefreshToken
} from '@/utils/auth'

const ScmsaService = new ScmsaAxios({
  getToken: () => getToken(),
  getTokenType: () => getTokenType(),
  getRefreshToken: () => getRefreshToken(),
  setToken: v => setToken(v),
  setTokenType: v => setTokenType(v),
  setRefreshToken: v => setRefreshToken(v),
  getStore: () => store,
  getTenantAccount: () => localStorage.tenant_accounts ? JSON.parse(localStorage.tenant_accounts).tenantId + ':' + JSON.parse(localStorage.tenant_accounts).accountId : '',
  Message,
  router,
  clientId: process.env.VUE_APP_client_id,
  clientSecret: process.env.VUE_APP_client_secret,
  oauthApi: process.env.VUE_APP_OAUTH_API,
  oauthAdminApi: process.env.VUE_APP_OAUTH_ADMIN_API
})

// create an axios instance
const service = ScmsaService.create({
  baseURL: process.env.VUE_APP_ADMIN_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: process.env.NODE_ENV === 'development' ? (600 * 1000) : (180 * 1000) // request timeout
})

export default service
export {
  ScmsaService
}
